/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.syr.iis.web.model.DialogueAct;
import edu.syr.iis.web.model.DialogueActLabel;
import edu.syr.iis.web.model.DialogueActManager;
import edu.syr.iis.web.model.User;
import edu.syr.iis.web.model.UserManager;

/**
 * @author kinoue
 *
 */
public class UserPropertyEditor extends PropertyEditorSupport
{
    UserManager manager = null;
    private static Log log = LogFactory.getLog(DialogueActPropertyEditor.class);

    public UserPropertyEditor(UserManager manager)
    {
	super();
	this.manager = manager;
    }	

    @Override
    public String getAsText() {			

	User value = (User) getValue();
	String text = "null";

	System.out.println("Entering getAsText() with " + value);

	if (value != null)
	    text = Integer.toString(value.getUserId());

	System.out.println("Returning: " + text);
	return text;
    }

    @Override
    public void setAsText(String text)  throws IllegalArgumentException 
    {
	System.out.println("Entering: setAsText() with: " + text);

	if (text.equals("null"))
	{
	    setValue(null);
	} 
	else
	{
	    int id = Integer.parseInt(text);
	    User value = manager.getUser(id);

	    if (value == null) {
		throw new IllegalArgumentException("No object found with id: " + id);
	    }
	    log.debug("  Setting value with \n" + value.getUserName());
	    setValue(value);
	}
    }


}
